package cn.weakup.designpattern.b_creational.factory.simplefactory;

/**
 * @author lixj
 * @version 1.0.0
 * @date 2022/6/2
 * @desc 优化：利用继承和多态让不同的处理逻辑分离为单独的类
 *             通过简单工厂模式来创建对应的实体类
 */
public abstract class Operation extends OperationFactory {
    private Double numberA;
    private Double numberB;

    public Double getNumberA() {
        return numberA;
    }

    public void setNumberA(Double numberA) {
        this.numberA = numberA;
    }

    public Double getNumberB() {
        return numberB;
    }

    public void setNumberB(Double numberB) {
        this.numberB = numberB;
    }

    /**
     * 由子类实现具体的运算逻辑
     * @return
     */
    abstract Double getResult();
    /**
     * 处理计算逻辑
     * @param numberA
     * @param numberB
     * @param option
     * @return
     */
    public static String handleProgram(String numberA, String numberB, String option) {
        Double result;
        Operation operation;
        try {
            /**
             * 利用简单工厂模式将创建对应的处理类的实例
             */
            operation = OperationFactory.createOperate(option);
            operation.setNumberA(Double.valueOf(numberA));
            operation.setNumberB(Double.valueOf(numberB));
            result = operation.getResult();
        } catch (Exception e) {
            return e.getMessage();
        }
        return result.toString();
    }
}
